#include <judge/sandbox.h>
#include <iostream>
#include <fstream>
#include <string>
#include <filesystem>

namespace fs = std::filesystem;

judge::ExecutionResult compileCode(const std::string& source_file, const std::string& output_file) {
    judge::Sandbox sandbox;
    
    judge::ResourceLimits compile_limits;
    compile_limits.memory_bytes = 512 * 1024 * 1024;  // 512MB
    compile_limits.cpu_time = std::chrono::milliseconds(10000);  // 10秒
    sandbox.setResourceLimits(compile_limits);
    
    // 设置编译命令
    sandbox.setCommand("/usr/bin/g++", {
        "-O2",
        "-Wall",
        "-std=c++17",
        source_file,
        "-o",
        output_file
    });
    
    return sandbox.execute();
}

// 运行编译后的程序
judge::ExecutionResult runProgram(const std::string& program_path, const std::string& input = "") {
    judge::Sandbox sandbox;
    
    // 设置运行资源限制
    judge::ResourceLimits run_limits;
    run_limits.memory_bytes = 256 * 1024 * 1024;  // 256MB
    run_limits.cpu_time = std::chrono::milliseconds(1000);  // 1秒
    sandbox.setResourceLimits(run_limits);
    
    // 如果有输入，创建输入文件
    if (!input.empty()) {
        std::ofstream input_file("input.txt");
        input_file << input;
        input_file.close();
        
        // 重定向标准输入
        sandbox.setCommand(program_path, {"<", "input.txt"});
    } else {
        sandbox.setCommand(program_path, {});
    }
    
    return sandbox.execute();
}

int main(int argc, char* argv[]) {
    if (argc < 2) {
        std::cerr << "Usage: " << argv[0] << " <source_file> [input]\n";
        return 1;
    }

    try {
        std::string source_file = argv[1];
        std::string input = argc > 2 ? argv[2] : "";
        
        // 检查源文件是否存在
        if (!fs::exists(source_file)) {
            std::cerr << "源文件不存在: " << source_file << "\n";
            return 1;
        }
        
        // 编译
        std::cout << "正在编译...\n";
        auto compile_result = compileCode(source_file, "./program");
        
        if (compile_result.exit_code != 0) {
            std::cout << "编译失败:\n" << compile_result.stderr_output << "\n";
            return compile_result.exit_code;
        }
        
        std::cout << "编译成功。\n\n";
        
        // 运行
        std::cout << "正在运行...\n";
        auto run_result = runProgram("./program", input);
        
        // 输出结果
        std::cout << "=== 运行结果 ===\n"
                  << "退出码: " << run_result.exit_code << "\n"
                  << "CPU时间: " << run_result.cpu_time.count() << "ms\n"
                  << "内存使用: " << run_result.memory_bytes / 1024 << "KB\n"
                  << "标准输出:\n" << run_result.stdout_output << "\n";
        
        if (!run_result.stderr_output.empty()) {
            std::cout << "标准错误:\n" << run_result.stderr_output << "\n";
        }
        
        if (!run_result.error_message.empty()) {
            std::cout << "错误信息: " << run_result.error_message << "\n";
        }
        
        // 清理临时文件
        fs::remove("./program");
        if (!input.empty()) {
            fs::remove("input.txt");
        }
        
        return run_result.exit_code;
    } catch (const std::exception& e) {
        std::cerr << "错误: " << e.what() << "\n";
        return 1;
    }
} 